package org.dllwh.mongodb;

import jakarta.annotation.Resource;
import org.dllwh.mongodb.model.SysUser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName SaveTest
 * @Description MongoTemplate 保存(插入)操作测试类
 * @Author: <a href="mailto:duleilewuhen@sina.com">独泪了无痕</a>
 * @Date 2025-05-10 00:05
 * @版本 V 1.0.1
 * @since JDK 17
 */
@SpringBootTest
public class SaveTest {

    @Resource
    private MongoTemplate mongoTemplate;
    private final String collectionName = "SaveTest";

    @BeforeEach
    public void init() {
        // 删除集合
        mongoTemplate.dropCollection(collectionName);
    }

    /**
     * 自定义集合，插入文档
     */
    @Test
    public void insertTest() {
        SysUser sysUser = new SysUser("12", "黄小飞", "女", 33);
        mongoTemplate.insert(sysUser, collectionName);
    }

    /**
     * 自定义集合，批量插入文档
     */
    @Test
    public void insertBatchTest() {
        List<SysUser> userList = new ArrayList<>();
        userList.add(new SysUser("1", "白鹿", "女", 31));
        userList.add(new SysUser("2", "陈都灵", "女", 32));
        userList.add(new SysUser("3", "高圆圆", "女", 46));
        userList.add(new SysUser("4", "张含韵", "女", 36));
        userList.add(new SysUser("5", "李沁", "女", 35));
        userList.add(new SysUser("6", "胡歌", "男", 43));
        userList.add(new SysUser("7", "刘亦菲", "女", 38));
        userList.add(new SysUser("8", "成龙", "男", 71));
        userList.add(new SysUser("9", "杨幂", "男", 39));
        userList.add(new SysUser("10", "陈钰琪", "女", 33));
        mongoTemplate.insert(userList, collectionName);
    }

    /**
     * 自定义集合，存储文档，如果没有插入，否则更
     */
    @Test
    public void saveTest() {
        SysUser sysUser = new SysUser("12", "黄小飞", "女", 33);
        mongoTemplate.save(sysUser, collectionName);
    }
}
